package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.wlwl.os.listbottomsheetdialog.MyDecoration;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.TestApplication;
import com.yanggeng.demo.beginnerguidedemo.adapter.SimpleAdapter;
import com.yanggeng.demo.beginnerguidedemo.bean.EventConstant;
import com.yanggeng.demo.beginnerguidedemo.bean.EventMessage;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 位置搜索
 * Created by yanggeng on 2017/9/19.
 */

public class LocationSearchActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener, Inputtips.InputtipsListener, SimpleAdapter.OnItemClickListener {

    private EditText etSearch;
    private TextView tvStop, tvOil, tvMaintenance, tvPolice, tvSearch;
    private RecyclerView rvSearch;
    private ImageView ivBack;

    private PoiSearch.Query query;
    private PoiSearch poiSearch;
    private Context context;
    private List<String> locations = new ArrayList<>();
    private List<Tip> tips = new ArrayList<>();
    private SimpleAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        hideToolbar();
        context = this;
        etSearch = (EditText) findViewById(R.id.et_search);
        tvStop = (TextView) findViewById(R.id.tv_stop);
        tvOil = (TextView) findViewById(R.id.tv_oil);
        tvMaintenance = (TextView) findViewById(R.id.tv_maintenance);
        tvPolice = (TextView) findViewById(R.id.tv_police);
        rvSearch = (RecyclerView) findViewById(R.id.rv_search);
        tvSearch = (TextView) findViewById(R.id.tv_search);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvSearch.setOnClickListener(this);
        tvStop.setOnClickListener(this);
        tvOil.setOnClickListener(this);
        tvMaintenance.setOnClickListener(this);
        tvPolice.setOnClickListener(this);
        ivBack.setOnClickListener(this);

        etSearch.addTextChangedListener(new TipsTextWatcher());

        rvSearch.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvSearch.setNestedScrollingEnabled(false);
        rvSearch.addItemDecoration(new MyDecoration(context));
        adapter = new SimpleAdapter(context, locations);
        adapter.setOnItemClickListener(this);
        rvSearch.setAdapter(adapter);
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        ArrayList<PoiItem> poiItems = poiResult.getPois();
        Intent intent = new Intent();
        intent.putExtra("data", (ArrayList<PoiItem>)poiItems);
        setResult(RESULT_OK, intent);
        for (PoiItem poiItem :
                poiItems) {
            Log.e("poiItem", "名称：" + poiItem.getTitle() + ", 经纬度" + poiItem.getLatLonPoint().getLatitude() + "," + poiItem.getLatLonPoint().getLongitude()
            + "，扩展信息：" + poiItem.getPoiExtension().getmRating() + poiItem.getPoiExtension().getOpentime() + "，地址：" + poiItem.getSnippet() + "，距离中心点：" + poiItem.getDistance());
        }
        finish();
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
    }

    @Override
    public void onGetInputtips(List<Tip> list, int i) {
        locations.clear();
        tips.clear();
        tips.addAll(list);
        Log.e("getInputTips", list.size() + "条数据，" + list.toString());
        for (int j = 0; j < list.size(); j ++) {
            locations.add(list.get(j).getName() + "\n" + list.get(j).getAddress());
        }
        list.get(0).getTypeCode();
        adapter.notifyData(locations);
    }

    @Override
    public void onItemClick(View v, int position) {
        EventBus.getDefault().post(new EventMessage(EventConstant.SEARCH_LOCATION, tips.get(position)));
        finish();
    }

    class TipsTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(s.toString().trim())) {
                locations.clear();
                tips.clear();
                adapter.notifyData(locations);
                tvSearch.setVisibility(View.GONE);
                return;
            }
            if (tvSearch.getVisibility() == View.GONE) {
                tvSearch.setVisibility(View.VISIBLE);
            }
            InputtipsQuery iq = new InputtipsQuery(s.toString().trim(), TestApplication.getApplication().getCity());
            iq.setCityLimit(true);
            iq.setLocation(new LatLonPoint(TestApplication.getApplication().getCurrLatLng().latitude, TestApplication.getApplication().getCurrLatLng().longitude));
            Inputtips inputtips = new Inputtips(context, iq);
            inputtips.setInputtipsListener(LocationSearchActivity.this);
            inputtips.requestInputtipsAsyn();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:
                search(etSearch.getText().toString().trim(), "");
                break;
            case R.id.tv_stop:
                //停车场
                search("停车场", "");//150900
                break;
            case R.id.tv_oil:
                search("", "010100");
                //加油站
                break;
            case R.id.tv_maintenance:
                //维修保养
                search("维修保养", "010000");
                break;
            case R.id.tv_police:
                //交警队
                search("交警队", "");
                break;
        }
    }

    private void search(String keyword, String keyCode) {
        query = new PoiSearch.Query(keyword, keyCode, TestApplication.getApplication().getCity());
        query.setPageSize(20);
        query.setPageNum(0);
        query.setDistanceSort(true);
        query.setLocation(new LatLonPoint(TestApplication.getApplication().getCurrLatLng().latitude, TestApplication.getApplication().getCurrLatLng().longitude));
        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }
}
